﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Cotizav.CONTROLADOR;
using Cotizav.PRESENTACION;
using System.Data.SqlClient;

namespace Cotizav
{
    public partial class Form1 : Form
    {
        CtrlPeriodo ctPeriodo;        
        
        public Form1()
        {
            InitializeComponent();
            ctPeriodo = new CtrlPeriodo();
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            backgroundWorkerPrincipal.RunWorkerAsync();
        }

        private bool verificarPeriodo()
        {
            //Verificacion del periodo
            bool result = ctPeriodo.esPeriodoActual();

            if (result)
            {
                return true;
            }
            else
            {
                return false;                
            }
        }

        private bool periodoNuevoEstablecido()
        {
            //se procede a cambiar el periodo
            bool result2 = ctPeriodo.establecerNuevoPeriodo();

            if (result2)
            {
                return true;                
            }
            else
            {
                return false;
            }
        }

        private void backgroundWorkerPrincipal_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                bool periodoActual = this.verificarPeriodo();
                backgroundWorkerPrincipal.ReportProgress(50, "Cargando...");
                System.Threading.Thread.Sleep(1000);  
         
                if (!periodoActual)
                {
                    //cambiamos el periodo
                    bool periodoEstablecido = this.periodoNuevoEstablecido();
                    backgroundWorkerPrincipal.ReportProgress(75, "Estableciendo nuevo periodo...");
                    System.Threading.Thread.Sleep(1000);                   

                    if (periodoEstablecido)
                    {                        
                        backgroundWorkerPrincipal.ReportProgress(100,"Cargando interfaz..."); 
                        System.Threading.Thread.Sleep(1000);                       
                    }
                    else
                    {
                        e.Result = 1; //error de registro en periodo
                        e.Cancel = true; 
                    }
                }
                else
                {
                    backgroundWorkerPrincipal.ReportProgress(100, "Cargando interfaz..."); 
                    System.Threading.Thread.Sleep(1000);
                }
            }
            catch (Exception)
            {
                e.Result = 0;
                e.Cancel = true;
            }
        }

        private void backgroundWorkerPrincipal_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progreso.Value = e.ProgressPercentage;
            txtProgreso.Text = e.UserState as string;
              
        }

        private void backgroundWorkerPrincipal_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled == false)
            {
                //si todo ha sido correcto
                //cargamos el form principal
                Principal mainform = new Principal();

                this.Hide();
                mainform.Show();                
            }
            else
            {
                int errorCode = (int)e.Result;

                switch (errorCode)
                {
                    case 0:
                        MessageBox.Show(this, "Error en tiempo de ejecución \nPor favor contactarse con el Administrador del sistema", "Cotizav", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                        Application.Exit();
                        break;
                    
                    case 1:
                        MessageBox.Show(this, "Error al registrar nuevo periodo \nPor favor contactarse con el Administrador del sistema", "Cotizav", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                        Application.Exit();
                        break;
                }
            }
        }
    }
}
